// import { formatDate } from 'xe-utils';

// 自定义属性装饰器示例
function Required(target: any, propertyKey: string) {
  let value = target[propertyKey];
  
  const getter = function() {
    return value;
  };
  
  const setter = function(newVal: any) {
    if (newVal === undefined || newVal === null) {
      throw new Error(`属性 ${propertyKey} 是必填的`);
    }
    value = newVal;
  };
  
  Object.defineProperty(target, propertyKey, {
    get: getter,
    set: setter,
    enumerable: true,
    configurable: true
  });
}

// 自定义方法装饰器示例
function Log(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
  const originalMethod = descriptor.value;
  
  descriptor.value = function(...args: any[]) {
    // console.log(`调用方法 ${propertyKey} 时间: ${formatDate(new Date(), 'yyyy-MM-dd HH:mm:ss')}`);
    // console.log(`参数: ${JSON.stringify(args)}`);
    
    const result = originalMethod.apply(this, args);
    
    // console.log(`结果: ${JSON.stringify(result)}`);
    return result;
  };
  
  return descriptor;
}

// 用户接口
export interface IUser {
  id?: number;
  username: string;
  email: string;
  age: number;
  createTime?: Date;
}

// 用户类
export class User implements IUser {
  @Required
  username: string;
  
  @Required
  email: string;
  
  age: number;
  id?: number;
  createTime: Date;
  
  constructor(data: IUser) {
    this.id = data.id;
    this.username = data.username;
    this.email = data.email;
    this.age = data.age;
    this.createTime = data.createTime || new Date();
  }
  
  @Log
  getDisplayName(): string {
    return `${this.username} (${this.email})`;
  }
  
  @Log
  updateAge(newAge: number): number {
    this.age = newAge;
    return this.age;
  }
  
  // 静态方法
  static createFromJSON(json: string): User {
    try {
      const data = JSON.parse(json);
      return new User(data);
    } catch (e) {
      console.error('解析JSON失败', e);
      throw new Error('无效的用户数据');
    }
  }
} 